package com.click.app.dao.hibernate;

import com.click.app.dao.PurchaseDetailDao;
import com.click.app.model.Material;
import com.click.app.model.PurchaseDetail;
import java.util.Date;
import java.util.List;

/**
 * @author Joel Chuca Condo
 * @date 04/05/2009
 */
public class PurchaseDetailDaoHibernate extends GenericDaoHibernate<PurchaseDetail, Long> implements PurchaseDetailDao {

    public PurchaseDetailDaoHibernate(){
        super(PurchaseDetail.class);
    }

    public List<PurchaseDetail> findByMaterial(Material material) {
        List<PurchaseDetail> purchasesDetails = getHibernateTemplate().find("from PurchaseDetail where material=? order by purchase.date", material);
        /*if(!purchasesDetails.equals(null))
            return purchasesDetails;
        else
            return new ArrayList<PurchaseDetail>();*/
        return purchasesDetails;
    }

    public List<PurchaseDetail> findByMaterial(Long id) {
        List<PurchaseDetail> purchasesDetails = getHibernateTemplate().find("from PurchaseDetail where material.id=? order by purchase.date", id);
        /*if(!purchasesDetails.equals(null))
            return purchasesDetails;
        else
            return new ArrayList<PurchaseDetail>();*/
        return purchasesDetails;
    }

    public List<PurchaseDetail> findByMaterialPeriod(Material material, Date initDate, Date endDate) {
        Object[] objects = {material, initDate, endDate};
        List<PurchaseDetail> purchasesDetails = getHibernateTemplate().find("from PurchaseDetail where material=? and purchase.date Between ? And ? order by purchase.date", objects);
        /*if(!purchasesDetails.equals(null))
            return purchasesDetails;
        else
            return new ArrayList<PurchaseDetail>();*/
        return purchasesDetails;
    }

    public List<PurchaseDetail> findByMaterialPeriod(Long id, Date initDate, Date endDate) {
        Object[] objects = {id, initDate, endDate};
        List<PurchaseDetail> purchasesDetails = getHibernateTemplate().find("from PurchaseDetail where material.id=? and purchase.date Between ? And ? order by purchase.date", objects);
        /*if(!purchasesDetails.equals(null))
            return purchasesDetails;
        else
            return new ArrayList<PurchaseDetail>();*/
        return purchasesDetails;
    }
    public List<Material> findByMaterialPeriod(){
        return getHibernateTemplate().find("select distinct purchaseDetail.material from PurchaseDetail purchaseDetail where size(purchaseDetail.packageDetails) = 0 ");
    }
    public List<PurchaseDetail> findByMaterialStockNoCero(Long id){
        return getHibernateTemplate().find("from PurchaseDetail where material.id=? and currentQuantity > 0 order by purchase.date desc ",id);
    }

}
